package org.wu.framework.core.io;

import org.wu.framework.core.io.ScriptResource;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

public class FileSystemScriptResource implements ScriptResource {

    private final String path;

    private final File file;

    private final Path filePath;

    public FileSystemScriptResource(String path) {
        this.path = path;
        this.file = new File(path);
        this.filePath = this.file.toPath();
    }

    /**
     * 脚本名称
     *
     * @return 脚本名称
     */
    @Override
    public String getPath() {
        return path;
    }

    /**
     * 获取 文件流文件
     *
     * @return 文件流文件
     * @throws IOException
     */
    @Override
    public InputStream getInputStream() throws IOException {
        try {
            return Files.newInputStream(this.filePath);
        }
        catch (NoSuchFileException ex) {
            throw new FileNotFoundException(ex.getMessage());
        }
    }
}
